﻿@using SDF.Services.Configuration;
@using SDF.Plugin.Misc.DynamicEntity;

@model  SDF.Plugin.Misc.DynamicEntity.Models.GenerateEntityModel

@{
    //page title
    ViewBag.Title = T("Plugins.Misc.DynamicEntity.GenEntity").Text;
    //active menu item (system name)
    //Html.SetActiveMenuItemSystemName("DynamicEntity");
    var _settingService = EngineContext.Current.Resolve<ISettingService>();
    var _settings = _settingService.LoadSetting<DynamicEntitySettings>();
    var defaultGridPageSize = EngineContext.Current.Resolve<SDF.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<SDF.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="content-header clearfix">
        <h1 class="pull-left">
            @T("Plugins.Misc.DynamicEntity.GenEntity")
            <small>
                <i class="fa fa-arrow-circle-left"></i>
                @Html.ActionLink(T("Plugins.Misc.DynamicEntity.BackToList").Text, "HeaderList")
            </small>
        </h1>
        <div class="pull-right">
            <button type="submit" name="save" class="btn bg-blue">
                <i class="fa fa-floppy-o"></i>
                @T("Admin.Common.Save")
            </button>
            <button type="submit" name="save-continue" class="btn bg-blue">
                <i class="fa fa-floppy-o"></i>
                @T("Admin.Common.SaveContinue")
            </button>
        </div>
    </div>
    <div class="content">
        <div class="form-horizontal">
            <div class="panel-group">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        @Html.SDFLabelFor(m => m.SAPInstance)
                                    </div>
                                    <div class="col-md-8">
                                        @Html.SDFDropDownListFor(m => m.SAPInstance, new List<SelectListItem>() { new SelectListItem() { Value = "0", Text = "请选择" } })
                                        @Html.ValidationMessageFor(m => m.SAPInstance)

                                        <script>
                                            $(function(){
                                                $.ajax({
                                                    type: "POST",
                                                    url: "@_settings.SAPInstanceToken",
                                                    data : addAntiForgeryToken,
                                                    success:function(data)
                                                    {
                                                        if(!data)
                                                            return;

                                                        $(data).each(function(index,item){
                                                            $("#@Html.FieldIdFor(m=>m.SAPInstance)").
                                                                append($("<option></option>").val(item.SAPInstance).html(item.Name));
                                                        });


                                                    }
                                                })
                                            });
                                        </script>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        @Html.SDFLabelFor(m => m.SAPFunctionName)
                                    </div>
                                    <div class="col-md-8">
                                        @Html.SDFEditorFor(m => m.SAPFunctionName)
                                        @Html.ValidationMessageFor(m => m.SAPFunctionName)
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-7 col-md-offset-5">
                                <button type="button" id="btn_fetch" class="btn btn-primary btn-search">
                                    <i class="fa fa-search"></i>
                                    @T("Plugins.Misc.DynamicEntity.Test")
                                </button>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="panel panel-default">
                    <div class="panel-body">
                        <div id="grid">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(function () {
            $("#btn_fetch").click(function (e) {
                e.preventDefault();

                var sapInstance = $("#@Html.FieldIdFor(m => m.SAPInstance)").val();
                var functionName = $("#@Html.FieldIdFor(m => m.SAPFunctionName)").val();
                if (!sapInstance || !functionName||sapInstance=="0")
                {
                    //尝试后台配置验证，前台校验
                    alert("请输入必填值");
                    return false;
                }

                showGrid(sapInstance,functionName);

                return false;
            });
        })


        function showGrid (sapInstance,functionName)
        {
            $("#grid").kendoGrid({
                dataSource: {
                    type: "json",
                    transport: {
                        read: {
                            url: '@_settings.SAPFunctionToken',
                            type: "POST",
                            dataType: "json",
                            data: {
                                SAPInstance: sapInstance,
                                FunctionName: functionName
                            }
                        }
                    },
                    schema: {
                        data: function (data) {
                            return data;
                        },
                        total: function (data) {
                            return data.length;
                        }
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    pageSize: @(defaultGridPageSize),
                },
                pageable: {
                    refresh: false,
                    pageSizes: [@(gridPageSizes)]
                },
                scrollable: false,
                columns: [
                    {
                        field: "ParamName",
                        title: "ParamName"
                    },
                    {
                        field: "ParamType",
                        title: "ParamType"
                    },
                    {
                        field: "ParamLength",
                        title: "ParamLength"
                    },
                    {
                        field: "Direction",
                        title: "Direction"
                    }
                ]
            });
        };
    </script>
}
